package com.example.demofdfs.sort;

import java.util.Arrays;

public class BubbleSort<T extends Comparable<T>> {

	public static void main(String[] args) {
		BubbleSort<String> stringSort = new BubbleSort<String>();
		String[] array = {"z", "c", "d", "s", "y", "u", "c", "a", "n"};
		System.out.println(Arrays.toString(array));
		stringSort.sort(array);
		System.out.println(Arrays.toString(array));
		
//		----------------------------------------
		
		BubbleSort<Integer> intSort = new BubbleSort<Integer>();
		Integer[] ints = {100,45,22,55,433,66,1};
		System.out.println(Arrays.toString(ints));
		intSort.sort(ints);
		System.out.println(Arrays.toString(ints));
	}
	
	public void sort(T[] array) {
		int length = array.length;
		if (length < 2)
			return ;
		for (int i = 0; i < length -1; i++) {
			for (int j = 0; j < length - 1 - i; j++) {
				swap(array, j);
			}
		}
	}
	
	public void swap(T[] array, int index) {
		if (array[index].compareTo(array[index + 1]) > 0) {
			T temp = array[index];
			array[index] = array[index + 1];
			array[index + 1] = temp;
		}
	}
	
}
